﻿using System;
using System.Text;
using System.Security.Cryptography;
using System.Text.RegularExpressions;

namespace patents_robot_demo.Analysis
{
    public static class Security
    {
        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="str">原字符串</param>
        /// <returns>加密后的字符串</returns>
        #region MD5加密
        public static string GetMD5(this string str)
        {
            using (MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider())
            {
                string ret = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(str)));
                ret = ret.Replace("-", "");
                return ret;
            }
        }
        #endregion

        #region 截取字符串
        /// <summary>
        /// 截取字符串（去HTML）
        /// </summary>
        /// <param name="str">String</param>
        /// <param name="len">长度</param>
        /// <returns>返回截取后的字符串</returns>
        public static string CutString(this string str, int len)
        {
            if (string.IsNullOrEmpty(str))
                return string.Empty;
            str = FormatHtml(str);
            if (str.Length > len)
                return str.Substring(0, len) + "...";
            else
                return str;
        }
        /// <summary>
        /// 截取字符串
        /// </summary>
        /// <param name="str"></param>
        /// <param name="len"></param>
        /// <returns></returns>
        public static string Cut(this string str, int len)
        {
            if (string.IsNullOrEmpty(str))
                return string.Empty;
            if (str.Length > len)
                return str.Substring(0, len);
            else
                return str;
        }
        #endregion

        #region 过滤html字符
        /// <summary>
        /// 过滤html字符
        /// </summary>
        /// <param name="html">原HTML</param>
        /// <returns>去掉HTML的</returns>
        public static string FormatHtml(this string html)
        {

            Regex regex1 = new Regex("<script[sS]+</script *>", RegexOptions.IgnoreCase);
            Regex regex2 = new Regex(" href *= *[sS]*script *:", RegexOptions.IgnoreCase);
            Regex regex3 = new Regex(" no[sS]*=", RegexOptions.IgnoreCase);
            Regex regex4 = new Regex("<iframe[sS]+</iframe *>", RegexOptions.IgnoreCase);
            Regex regex5 = new Regex("<frameset[sS]+</frameset *>", RegexOptions.IgnoreCase);
            Regex regex6 = new Regex("<img[^>]+>", RegexOptions.IgnoreCase);
            Regex regex7 = new Regex("</p>", RegexOptions.IgnoreCase);
            Regex regex8 = new Regex("<p>", RegexOptions.IgnoreCase);
            Regex regex9 = new Regex("<[^>]*>", RegexOptions.IgnoreCase);
            html = regex1.Replace(html, "");
            html = regex2.Replace(html, "");
            html = regex3.Replace(html, " _disibledevent=");
            html = regex4.Replace(html, "");
            html = regex5.Replace(html, "");
            html = regex6.Replace(html, "");
            html = regex7.Replace(html, "");
            html = regex8.Replace(html, "");
            html = regex9.Replace(html, "");
            html = html.Replace("&nbsp;", "");
            html = html.Replace(" ", "");
            html = html.Replace("</strong>", "");
            html = html.Replace("<strong>", "");
            html = html.Replace("\r\n", "");
            html = html.Replace("\n\r", "");
            html = html.Replace("\r", "");
            html = html.Replace("\n", "");
            html = html.Replace("\t", "");
            html = html.Replace(Environment.NewLine, "");
            return html;
        }
        #endregion

        #region 转换HTML
        /// <summary>
        /// Html转换
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string ConvertSqlContenttoHtml(this string str)
        {
            str = str.Replace(" ", "&nbsp;");
            str = str.Replace("\n", "<br />");
            return str;
        }
        #endregion
        #region 验证中文；
        static public bool Regular_IsChinese(this string str)
        {
            return System.Text.RegularExpressions.Regex.IsMatch(str, @"[\u4e00-\u9fa5]");
        }
        #endregion
        /// <summary>
        /// 进行GB2312编码
        /// </summary>
        /// <param name="str">原字符串</param>
        /// <returns>进行GB2312编码后的字符串</returns>
        #region GB2312编码
        static public string GetGB2312(string str)
        {
            StringBuilder strResult = new StringBuilder();
            Byte[] encodedBytes = new byte[str.Length * 2];
            int iCount = Encoding.GetEncoding("gb2312").GetBytes(str, 0, str.Length, encodedBytes, 0);
            foreach (Byte b in encodedBytes)
            {
                strResult.Append("%" + Convert.ToString(b, 16).ToUpper());
            }
            return strResult.ToString();
        }
        #endregion
    }
}
